package com.link.base.base.publicdataimport.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.publicdataimport.model.Publicdataimport;
import com.link.base.base.publicdataimport.service.PublicdataimportService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 黄安邦
 * @date 2018-06-22
 */
@Controller
@RequestMapping("/link/publicdataimport")
public class PublicdataimportController extends BasicController<Publicdataimport> {


    @Resource
    private PublicdataimportService publicdataimportService;

    @Resource
    private KeyGenerateService keyGenerateService;

    @Override
    public BasicService<Publicdataimport> getBasicService() throws Exception {
        return publicdataimportService;
    }


    /**
     * @param
     * @return
     * @auther Luoxuliang
     * @date 2018/11/22 7:27 PM
     * @comments 更新ID到对象
     */
    @RequestMapping(value = "/updateModelById")
    @ResponseBody
    public Map<String, Object> updateModelById(@JsonParam Publicdataimport entity, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(16);
        try {
            Publicdataimport tmpObject = publicdataimportService.queryById(entity);
            tmpObject.setAttachmentId(entity.getIsBackImport());
            publicdataimportService.update(tmpObject);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * @param entity
     * @param request
     * @throws Exception
     * @auther Luoxuliang
     * @date 2018/11/27 1:36 AM
     * @comments 获取组织ID用于权限查询
     */
    public void beforQueryExample(Publicdataimport entity, HttpServletRequest request) throws Exception {
        CoreUser  user = UserUtil.getUser();
        entity.setOrgId(user.getOrgId());
    }

    /**
     * @param entity
     * @param request
     * @throws Exception
     * @auther Luoxuliang
     * @date 2018/11/27 2:00 AM
     * @comments 获取组织和职位ID插入数据库
     */
    public void beforUpsert(Publicdataimport entity, HttpServletRequest request) throws Exception {
        CoreUser user = UserUtil.getUser();
        entity.setOrgId(user.getOrgId());
        entity.setPostnId(user.getPostnId());
    }

    /**
     * @param entity
     * @param request
     * @throws Exception
     * @auther Luoxuliang
     * @date 2018/12/13 15:50 PM
     * @comments 通用导入列表点击复制的后端接口
     */
    @RequestMapping(value = "/copyObject")
    @ResponseBody
    public Map<String, Object> copyObject(@JsonParam Publicdataimport entity, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Publicdataimport tmpObject = publicdataimportService.queryById(entity);
            tmpObject.setId(keyGenerateService.keyGenerate());
            publicdataimportService.insert(tmpObject);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
